﻿using MediatR;
using SmartCity.Passport.API.Application.Commands;
using SmartCity.Passport.API.DTO;
using SmartCity.Passport.API.ErrorCode;
using SmartCity.Passport.Domain.Entites;
using SmartCity.Passport.ErrorCode;
using SmartCity.Passport.Infrastructure.Interfaces;

namespace SmartCity.Passport.API.Application.CommandHandlers
{
    /// <summary>
    /// 部门用户查询命令处理
    /// </summary>
    public class DepartmentUserQueryCommandHandler:IRequestHandler<DepartmentUserQueryCommand, ApiResult<List<UserDto>>>
    {
        private readonly IBaseRepository<DepartmentUser> depaetuserRep;
        private readonly IBaseRepository<User> userRep;

        public DepartmentUserQueryCommandHandler(IBaseRepository<DepartmentUser> depaetuserRep,IBaseRepository<User> userRep)
        {
            this.depaetuserRep = depaetuserRep;
            this.userRep = userRep;
        }
        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">请求</param>
        /// <returns>返回用户列表任务</returns>
        public Task<ApiResult<List<UserDto>>> Handle(DepartmentUserQueryCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<UserDto>> result = new ApiResult<List<UserDto>>();
            result.code = ApiEnum.SearchSuccess;
            result.message = "查询成功";

            var list = from a in userRep.GetAll()
                       join b in depaetuserRep.GetAll() on a.UserId equals b.UserId
                       where b.DepartmentId == request.DepartmentId
                       select new UserDto
                       {
                           UserId = a.UserId,
                           UserNickname = a.UserNickname,
                       };
            result.data = list.ToList();

            return Task.FromResult(result);
        }
    }
}
